<?php

class Silk_Zgb_Adminhtml_ManufacturerController extends Mage_Adminhtml_Controller_Action
{
	protected function _initAction()
	{
			$this->loadLayout()->_setActiveMenu("zgb/manufacturer")->_addBreadcrumb(Mage::helper("adminhtml")->__("Manufacturer  Manager"),Mage::helper("adminhtml")->__("Manufacturer Manager"));
			return $this;
	}
	public function indexAction() 
	{
		    $this->_title($this->__("轻松老板"));
		    $this->_title($this->__("厂商管理"));

			$this->_initAction();
			$this->renderLayout();
	}
	public function editAction()
	{			    
		    $this->_title($this->__("轻松老板"));
			$this->_title($this->__("厂商"));
		    $this->_title($this->__("修改"));
			
			$id = $this->getRequest()->getParam("id");
			$model = Mage::getModel("zgb/manufacturer")->load($id);
			if ($model->getId()) {
				Mage::register("manufacturer_data", $model);
				$this->loadLayout();
				$this->_setActiveMenu("zgb/manufacturer");
				$this->_addBreadcrumb(Mage::helper("adminhtml")->__("厂商管理"), Mage::helper("adminhtml")->__("厂商管理"));
				$this->_addBreadcrumb(Mage::helper("adminhtml")->__("厂商信息"), Mage::helper("adminhtml")->__("厂商信息"));
				$this->getLayout()->getBlock("head")->setCanLoadExtJs(true);
				$this->_addContent($this->getLayout()->createBlock("zgb/adminhtml_manufacturer_edit"))->_addLeft($this->getLayout()->createBlock("zgb/adminhtml_manufacturer_edit_tabs"));
				$this->renderLayout();
			} 
			else {
				Mage::getSingleton("adminhtml/session")->addError(Mage::helper("zgb")->__("厂商不存在."));
				$this->_redirect("*/*/");
			}
	}

	public function newAction()
	{

	$this->_title($this->__("轻松老板"));
	$this->_title($this->__("厂商"));
	$this->_title($this->__("新建"));

    $id   = $this->getRequest()->getParam("id");
	$model  = Mage::getModel("zgb/manufacturer")->load($id);

	$data = Mage::getSingleton("adminhtml/session")->getFormData(true);
	if (!empty($data)) {
		$model->setData($data);
	}

	Mage::register("manufacturer_data", $model);

	$this->loadLayout();
	$this->_setActiveMenu("zgb/manufacturer");

	$this->getLayout()->getBlock("head")->setCanLoadExtJs(true);

	$this->_addBreadcrumb(Mage::helper("adminhtml")->__("厂商管理"), Mage::helper("adminhtml")->__("厂商管理"));
	$this->_addBreadcrumb(Mage::helper("adminhtml")->__("厂商信息"), Mage::helper("adminhtml")->__("厂商信息"));


	$this->_addContent($this->getLayout()->createBlock("zgb/adminhtml_manufacturer_edit"))->_addLeft($this->getLayout()->createBlock("zgb/adminhtml_manufacturer_edit_tabs"));

	$this->renderLayout();

	}
	public function saveAction()
	{

		$post_data=$this->getRequest()->getPost();
		if ($post_data) {
			try {
				$model = Mage::getModel("zgb/manufacturer")
				->addData($post_data)
				->setId($this->getRequest()->getParam("id"));
				$model->save();

				Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已保存！"));
				Mage::getSingleton("adminhtml/session")->setManufacturerData(false);

				if ($this->getRequest()->getParam("back")) {
					$this->_redirect("*/*/edit", array("id" => $model->getId()));
					return;
				}

				$this->_redirect("*/*/");
				return;
			} 
			catch (Exception $e) {
				Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
				Mage::getSingleton("adminhtml/session")->setManufacturerData($this->getRequest()->getPost());
				$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
			return;
			}

		}
		$this->_redirect("*/*/");
	}



	public function deleteAction()
	{
			if( $this->getRequest()->getParam("id") > 0 ) {
				try {
					$model = Mage::getModel("zgb/manufacturer");
					$model->setId($this->getRequest()->getParam("id"))->delete();
					Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已删除！"));
					$this->_redirect("*/*/");
				} 
				catch (Exception $e) {
					Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
					$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
				}
			}
			$this->_redirect("*/*/");
	}

	
	public function massRemoveAction()
	{
		try {
			$ids = $this->getRequest()->getPost('ids', array());
			foreach ($ids as $id) {
                  $model = Mage::getModel("zgb/manufacturer");
				  $model->setId($id)->delete();
			}
			Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已删除！"));
		}
		catch (Exception $e) {
			Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
		}
		$this->_redirect('*/*/');
	}
		
	/**
	 * Export order grid to CSV format
	 */
	public function exportCsvAction()
	{
		$fileName   = 'manufacturer.csv';
		$grid       = $this->getLayout()->createBlock('zgb/adminhtml_manufacturer_grid');
		$this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
	} 
	/**
	 *  Export order grid to Excel XML format
	 */
	public function exportExcelAction()
	{
		$fileName   = 'manufacturer.xml';
		$grid       = $this->getLayout()->createBlock('zgb/adminhtml_manufacturer_grid');
		$this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
	}

	protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('zgb');
    }
}
